/**
 * related to detail.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-16
 */
var page = sm("do_Page");
var app = sm("do_App");
var do_External = sm("do_External");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var orderUtil = require("url/order");
var shareUtil = require("url/share");
var shareHUtil = require("helper/share");
var httpSilence = require("helper/httpSilence");
var qqHttp = require("helper/http");
var openHelper = require("helper/open");
var helperOrder = require("helper/order");
var do_LinearLayout_item = ui("do_LinearLayout_item");
var do_LinearLayout_1 = ui("do_LinearLayout_1");

var labelOrderStatus = ui("labelOrderStatus");
var iconBg = ui("iconBg");
var do_Label_consignee = ui("do_Label_consignee");
var do_Label_phone = ui("do_Label_phone");
var do_Label_address = ui("do_Label_address");
var do_Label_seller = ui("do_Label_seller");
var do_Label_total = ui("do_Label_total");
var do_ALayout_freight = ui("do_ALayout_freight");
var do_Label_freight = ui("do_Label_freight");
var do_ALayout_sales = ui("do_ALayout_sales");
var do_Label_sales = ui("do_Label_sales");

var do_Label_orderNo = ui("do_Label_orderNo");
var do_Label_strNewAt = ui("do_Label_strNewAt");
var do_Label_payStatusAt = ui("do_Label_payStatusAt");
var do_Label_orderStatusAt = ui("do_Label_orderStatusAt");
var do_Label_deliveryAt = ui("do_Label_deliveryAt");
var do_Label_deliveryInfo = ui("do_Label_deliveryInfo");
var do_Label_returnPay = ui("do_Label_returnPay");

var do_Button_pay = ui("do_Button_pay");
var do_Button_cancel = ui("do_Button_cancel");
var do_Button_comment = ui("do_Button_comment");
var do_Button_complete = ui("do_Button_complete");
var do_Button_returnPay =ui("do_Button_returnPay");
var layoutBtn = ui("layoutBtn");
var button_phone = ui("button_phone");
var do_Button_service = ui("do_Button_service");
var label_remark = ui("label_remark");
var label_payTypeStr = ui("label_payTypeStr");
var do_GridView_coupon=ui("do_GridView_coupon");
var sendDeliveryName=ui("sendDeliveryName");
var sendDeliveryType=ui("sendDeliveryType");
var rootview = ui("$");
var addShare = rootview.add("share","source://view/reuse/share.ui", 0, 0);
/*二维码*/
var cacher = deviceone.sm("do_DataCache");
var qRCode = deviceone.sm("do_QRCode");
//andriod返回键
page.on("back", function() {
	/*var fromPage = page.getData().fromPage;
	if(fromPage == 'pay_success'){
		//关闭本页面和下层2个支付页面  
		app.closePage({fromPage:"order_detail"},"",3);
	}else if(fromPage == 'pay_main'){
		//关闭本页面和下层支付页面  
		app.closePage({fromPage:"order_detail"},"",3);
	}else{
		app.closePage({fromPage:"order_detail"});
	}*/
	/*app.closePageToID({}, "", "pay_product_back", function(data, e) {
    });*/
	app.closePage();
});

page.on("result", function(data) {
	if(data && data.feedback_add && data.feedbackSuccess){
		do_Button_comment.visible = false//成功添加评价
	}
});


var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	/*app.closePageToID({}, "", "pay_product_back", function(data, e) {
    });*/
	app.closePage();
});

//右上角-
var headerRight = ui(addheader + ".headerRight");
// 右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/share.png";
imgNext.visible = false;


var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "订单详情"
})
header.refreshData();

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var orderNo = page.getData().orderNo;
	getData(orderNo);
	createQrCode4Order(orderNo);//生成二维码
});
var order;
function getData(orderNo) {
	qqHttp.post(orderUtil.url.base.detail, {orderNo:orderNo}, function(data) {
		order = data.order;
		var delivery = data.delivery;
		var itemList = data.itemList;
		labelOrderStatus.text = order.orderStatusText;
		label_remark.text = order.remark;
		do_Button_complete.visible = order.showComplete;
		do_Button_cancel.visible = order.showCancel;
		do_Button_pay.visible = order.showPay;
		do_Button_comment.visible = order.showFeedback;
		do_Button_returnPay.visible = order.showReturnPay;
		layoutBtn.visible = order.hasBtn;
		if (order.orderStatus == 3 || order.orderStatus == 3) {
			iconBg.source="source://image/order/close.png";
		} else if (order.orderStatusText == "待付款") {
			iconBg.source="source://image/order/wait.png";
		} else if (order.orderStatusText == "交易完成") {
			iconBg.source="source://image/order/wc.png";
		}
		sendDeliveryName.text=data.sendDeliveryName;
		do_Button_service.tag=data.sendDeliveryPhone;
		sendDeliveryType.text=data.deliveryType;
		/*收货地址*/
		if(null!=delivery){
			if(delivery.type == 4){
				do_Label_consignee.text = "联系人："+delivery.consignee;
				do_Label_phone.text = delivery.phone;
				do_Label_address.text = "自提地址："+delivery.address;
			}else{
				do_Label_consignee.text = "收货人："+delivery.consignee;
				do_Label_phone.text = delivery.phone;
				do_Label_address.text = "收货地址："+delivery.address;
				if(delivery.type == 1){//卖家发货类型，配送费
					do_ALayout_freight.visible = true;
					do_Label_freight.text = "￥" + delivery.freight;
				}
				ui("orderCode").visible = false;
				
			}
		}
		if(order.subsidy > 0){
			do_ALayout_sales.visible = true;
			do_Label_sales.text = "-￥"+order.subsidy;
		}else{
			do_ALayout_sales.visible.visible = false;
		}
		if (order.orderType != 1) { //服务的不给打电话
			button_phone.tag = data.sellerPhone;
		}
		// 家政订单 加服务时间
		if (order.orderType == 1) {
			do_Button_complete.text = "确认服务";
			do_LinearLayout_1.add("serviceTime","source://view/order/homemaking/serviceTime.ui","do_ALayout_address");
			var serviceTime = ui("serviceTime");
			serviceTime.bindData(hashdata);
			if(null!=data.homemakingOrder){
				hashdata.addData({
					"serviceTimeStr" : data.homemakingOrder.serviceDateStr + " " + data.homemakingOrder.serviceHourStr
				});
			}
			serviceTime.refreshData();
			ui("do_ALayout_24").visible = false;
			ui("sendDelivery").visible = false;
		} else if (order.orderType == 5) { //代收代送
			var deliveryOrder = data.deliveryOrder;
			var deliveryNoUi = do_LinearLayout_1.add("serviceTime","source://view/order/homemaking/serviceTime.ui","do_ALayout_address");
			ui(deliveryNoUi+".do_Label_1").text = '物流号：';
			var serviceTime = ui("serviceTime");
			serviceTime.bindData(hashdata);
			hashdata.addData({
				"serviceTimeStr" : deliveryOrder.deliveryName +' '+deliveryOrder.deliveryNo
			});
			serviceTime.refreshData();
			ui("do_ALayout_24").visible = false;
			ui("sendDelivery").visible = false;
		}
		/*订单商品*/
		for(var i=0;i<itemList.length;i++) {
			var addItem = do_LinearLayout_item.add("item"+i, "source://view/order/detailTemplate.ui");
			var orderItemUi = ui("item"+i);
			orderItemUi.bindData(hashdata);
			hashdata.addData({
				"orderType" : order.orderType,
			    "productId" : itemList[i].productId,
			    "productName" :itemList[i].name + " " + itemList[i].productSpecName,
			    "productPrice" : "￥" + itemList[i].price,
			    "productCount" : itemList[i].quntity,
			    "productImg" : itemList[i].pathImg
			});
			orderItemUi.refreshData();
		}
		do_Label_seller.text = data.sellerName;
		do_Label_total.text = "￥" + order.total;
		do_Label_orderNo.text = "订单号："+order.orderNo;
		do_Label_strNewAt.text = "下单时间："+order.strNewAt;
		if (order.payStatus == 2) {
			do_Label_payStatusAt.visible = true;
			do_Label_payStatusAt.text = "付款时间："+order.strPayStatusAt; // 付款时间
		}
		if (order.deliveryStatus != 1) {
			do_Label_deliveryAt.visible = true;
			do_Label_deliveryAt.text = "发货时间："+order.strDeliveryStatusAt; // 发货时间		
			if (delivery.type == 1) {
				if (delivery.hasNeedDelivery == 1) {
					do_Label_deliveryInfo.visible = true;
					do_Label_deliveryInfo.text = "物流运单："+ delivery.deliveryName +' '+delivery.deliveryNo;
				}
			}
		}
		if (order.returnPayStatus != 1) {
			do_Label_returnPay.visible = true;
			do_Label_returnPay.text = "申请退款："+order.strReturnPayStatusAt;
		}
		if (order.orderStatus == 2) {
			if (order.strOrderStatusAt) {
				do_Label_orderStatusAt.visible = true;
			}
			do_Label_orderStatusAt.text = "完成时间："+order.strOrderStatusAt; // 完成时间
		} else if (order.orderStatus == 3) {
			do_Label_orderStatusAt.visible = true;
			do_Label_orderStatusAt.text = "取消时间："+order.strOrderStatusAt;
		}
		if (order.payType == 1) {
			label_payTypeStr.text = "货到付款";
		} else {
			label_payTypeStr.text = "在线支付";
		}
		do_GridView_coupon.items=data.orderCouponVoList;
		if(order.payStatus==2){
			imgNext.visible=true;
		}
		ui("do_LinearLayout_2").redraw();
	});
}


do_Button_pay.on("touch", function(data){
	if (order.payStatus == 1) {
		helperOrder.toPay(order.orderNo);
	}
});

do_Button_comment.on("touch", function(data){
	openHelper.open("source://view/order/comment/listAdd.ui", {orderNo : order.orderNo});
});
do_Button_complete.on("touch", function(data){
	if (order.deliveryStatus == 2) {
		nf.confirm("确认已收到商品?", "", "确定", "取消", function(data, e) {
			// 确认收货
			helperOrder.buyerSign(order.orderNo,function(data){
				do_Button_complete.visible = false;
				//订单已完成
				labelOrderStatus.text = "交易完成";
				iconBg.source="source://image/order/wc.png";
				do_Label_payStatusAt.visible = true;
				do_Label_orderStatusAt.visible = true;
				do_Button_complete.visible = false;
				do_Button_cancel.visible = false;
				do_Button_returnPay.visible = false;
				layoutBtn.visible = true;
				if(data.orderBase != null && data.orderBase.orderType != 4){
					do_Button_comment.visible = true;
					openHelper.open("source://view/order/comment/listAdd.ui", {orderNo : order.orderNo});
				}
			});
		});
	}
});
do_Button_cancel.on("touch", function(data){
	var orderNo = order.orderNo;
	var orderType = order.orderType;
	var payStatus = order.payStatus;
	var cancelEnable = order.cancelEnable;
	var msg = "确定取消订单?";
	if(orderType == 2 && payStatus == 2){
		msg = "订单取消后，支付金额将退还至圈圈生活个人账户余额";
	}
	nf.confirm(msg, "", "确定", "我再想想", function(data, e) {
		if (data == 1) {
			if(order.orderType == 1){
				helperOrder.buyerCancel(orderNo);
				iconBg.source="source://image/order/close.png";
				labelOrderStatus.text = "已取消";
				layoutBtn.visible = false;
			}else if(order.orderType == 2){
				if(cancelEnable == 1){
					helperOrder.cancelMallOrder(orderNo);//商城订单
					iconBg.source="source://image/order/close.png";
					labelOrderStatus.text = "已取消";
					layoutBtn.visible = false;
				}else{
					nf.alert("请联系客服取消订单");
				}
			}else if(order.orderType == 4){//活动
				helperOrder.buyerCancel(orderNo);
				iconBg.source="source://image/order/close.png";
				labelOrderStatus.text = "已取消";
				layoutBtn.visible = false;
			}else if(order.orderType == 3){//闲置品
				helperOrder.buyerCancel(orderNo);
				iconBg.source="source://image/order/close.png";
				labelOrderStatus.text = "已取消";
				layoutBtn.visible = false;
			}
		}
	});
});
button_phone.on("touch", function(data){
	if (button_phone.tag != null && button_phone.tag != '') {
		do_External.openDial(button_phone.tag);
	}else{
		nf.toast("暂无商家电话信息");
	}
});
do_Button_service.on("touch", function(data){
	//do_External.openDial("18948328120");
	if (do_Button_service.tag != null && do_Button_service.tag != '') {
		do_External.openDial(do_Button_service.tag);
	}else{
		nf.toast("订单尚未配送");
	}
});

do_Button_returnPay.on("touch", function(data){
	var payStatus = order.payStatus;
	if (payStatus == 2) {
		nf.confirm("您的退款申请将提交给商家，建议联系商家协商处理。如因您自身原因导致的申请，商家和客服有权拒绝。", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				var orderNo = order.orderNo;
				helperOrder.applyReturnPay(orderNo,function (){
					labelOrderStatus.text = "申请退款";
					do_Button_returnPay.visible = false;
					do_Button_cancel.visible = false;
					do_Button_complete.visible = false;
				});
			}
		});
	}
});
headerRight.on("touch", "", 300, function() {
	if(imgNext.visible){
		page.fire("onShare",{showView:"true",shareTitle:"圈圈生活买东西，就是这么便捷",shareContent:"下载注册圈圈生活，更多便捷和优惠等着您。",shareId:order.id,shareType:shareHUtil.type.SHARE_ORDER,shareImage:""});
	}
});

//生成订单二维码
function createQrCode4Order(vsOrderNo) {
	/** 二维码缓存的key，后面要跟业务类型+id，如QRCode-A-1 **/
	var mapKey_qrCode = "QRCode-mall-order-"+vsOrderNo;
	var paramsStr = "http://a.app.qq.com/o/simple.jsp?pkgname=com.qqtech.qquan.order"+"&bizzType=ORDER&bizzId="+vsOrderNo;
	if(cacher.hasData(mapKey_qrCode)){
		var imgUrl = cacher.loadData(mapKey_qrCode);
		if(!imgUrl || imgUrl==null || imgUrl==="undefined"){
			ui("qrCode").source = imgUrl;
		}else{
			qRCode.create(paramsStr,function(data,e){
				ui("qrCode").source = data;
				cacher.removeData(mapKey_qrCode);
				cacher.saveData(mapKey_qrCode,data);
			});
		}
	}else{
		qRCode.create(paramsStr,function(data,e){
			ui("qrCode").source = data;
			cacher.removeData(mapKey_qrCode);
			cacher.saveData(mapKey_qrCode,data);
		});
	}
	ui("do_ALayout_26").on("touch", function(data, e) {
		var qrImg = ui("qrCode").source;
		if(null!=qrImg&&''!=qrImg){
			var jsonImgs=[];
			jsonImgs.push({source:qrImg});
			openHelper.open("source://view/reuse/imgBrowser.ui", {
				imgDatas :JSON.stringify(jsonImgs),index:0
			});
		}
	});
}